package com.example.frameboot.controller;

import com.example.frameboot.been.*;
import com.example.frameboot.common.AutoMapper;
import com.example.frameboot.common.BaseResp;
import com.example.frameboot.common.ExcelUtil;
import com.example.frameboot.common.PageResp;
import com.example.frameboot.service.TestService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("api")
@Api(tags = "接口1")
public class TestController {
    @Autowired
    private TestService testService;

    @ApiOperation(value = "学生列表", notes = "学生列表")
    @GetMapping("/getList")
    public List<StudentResp> getList(){
        List<Student> dto =  testService.getList();
        List<StudentResp> resp = AutoMapper.batchTransform(StudentResp.class,dto);
        return resp;
    }
    @ApiOperation(value = "学生新增", notes = "学生新增")
    @PostMapping("/add")
    public BaseResp insertStu(@Valid StudentDto dto){
        testService.insertStu(dto);
        return new BaseResp();
    }
    @ApiOperation(value = "学生删除", notes = "学生删除")
    @DeleteMapping("/delete")
    public BaseResp deleteStu(@RequestParam(value = "id",required=true)List<String> id){
        testService.deleteStu(id);
        return new BaseResp();
    }
    @ApiOperation(value = "学生列表(分页)", notes = "学生列表(分页)")
    @GetMapping("/getStudent")
    public PageResp<StudentResp> getStudent(StudentReq req){
        PageInfo<Student> result = testService.getStudent(req);
        PageDto p = PageDto.builder().pageIndex(req.getPageIndex()).pageSize(req.getPageSize()).total(result.getTotal()).build();
        List<StudentResp> resps =AutoMapper.batchTransform(StudentResp.class,result.getList());
        PageResp<StudentResp> pageResp = new PageResp<StudentResp>(resps,p);
        return pageResp;
    }
    @ApiOperation(value = "学生新增", notes = "学生新增")
    @PostMapping("/addList")
    public BaseResp insertStuList(){
        testService.insertStuList();
        return new BaseResp();
    }

    @GetMapping("/exportQuick")
    public void exportQuick(HttpServletResponse response,
                            @RequestParam(value = "queueSize", defaultValue = "2") int queueSize,
                            @RequestParam(value = "pageSize", defaultValue = "10000") int pageSize) throws IOException {
        ExcelUtil.download(response, "测试")
                .parallel(queueSize)
                .pageExcelWriter(Student.class, page -> testService.lambdaQuery().page(page.setSize(pageSize)));
    }

    @ApiOperation(value = "学生新增", notes = "学生新增")
    @PostMapping("/addTest")
    public BaseResp insertStuTest(@Valid StudentDto dto){
        testService.insertStuTest(dto);
        return new BaseResp();
    }

}
